//
//  CollectVC.m
//  echo
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CollectVC.h"
#import <DKNightVersion.h>
#import "CellForSearchVC.h"
#import "CellForMyExploreTableViewSectionThreeAndFour.h"
#import "SQLHandler.h"
#import "ModelOfCellForMyExploreTableViewSectionThreeAndFour.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "CellForCollect.h"
#import "CellForCollectIsNull.h"

@interface CollectVC ()<UITableViewDataSource, UITableViewDelegate, CellForMyExploreTableViewSectionThreeAndFour>
@property (nonatomic, retain)UIImageView *imageViewForHeader;
@property (nonatomic, retain)UILabel *labelForCollect;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIButton *buttonForBack;
@property (nonatomic, retain)NSMutableArray *arrForMyExploreVC;
@property (nonatomic, retain)NSMutableArray *arrForClassifyVC;
@end

@implementation CollectVC

- (void)dealloc{
    [_imageViewForHeader release];
    [_labelForCollect release];
    [_buttonForBack release];
    [_arrForMyExploreVC release];
    [_arrForClassifyVC release];
    self.tableView.delegate = nil;
    [_tableView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"alreadyDeleteForCollect" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self handleData];
    
}

- (void)handleData{
    
    self.arrForMyExploreVC = [NSMutableArray array];
    self.arrForClassifyVC = [NSMutableArray array];
    NSArray *arr1 = [SQLHandler searchData];
    NSArray *arr2 = [SQLHandler searchDataForClassifyVC];
    for (ModelOfCellForMyExploreTableViewSectionThreeAndFour *model in arr1) {
        [self.arrForMyExploreVC addObject:model];
    }
    for (ModelOfCellForMyExploreTableViewSectionThreeAndFour *model in arr2) {
        [self.arrForClassifyVC addObject:model];
    }

    [self createTableView];
}

- (void)createTableView{

    self.imageViewForHeader = [[UIImageView alloc]init];
    self.imageViewForHeader.frame = CGRectMake(0, 0, self.view.frame.size.width, 240 * HEIGHT);
    [self.view addSubview:self.imageViewForHeader];
//    [self.imageViewForHeader sd_setImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201403/08/20140308201348_BcLrn.thumb.600_0.jpeg"]];
    self.imageViewForHeader.image = [UIImage imageNamed:@"shezhi2"];
    
    self.labelForCollect = [[UILabel alloc]init];
    self.labelForCollect.text = @"我的收藏";
    self.labelForCollect.dk_textColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor whiteColor]);
    self.labelForCollect.frame = CGRectMake(self.view.frame.size.width / 2 - 40 * WIDTH, 15 * HEIGHT, 80 * WIDTH, 50 * HEIGHT);
    [self.view addSubview:self.labelForCollect];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 240 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 240 * HEIGHT) style:UITableViewStyleGrouped];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[CellForCollect class] forCellReuseIdentifier:@"poolForCollect"];
    [self.tableView registerClass:[CellForCollectIsNull class] forCellReuseIdentifier:@"poolForCollectIsNull"];
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.buttonForBack];
    [self.buttonForBack setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    self.buttonForBack.frame = CGRectMake(5 * WIDTH, 20 * HEIGHT, 35 * WIDTH, 35 * WIDTH);
    [self.buttonForBack addTarget:self action:@selector(handleBack) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"alreadyDeleteForCollect" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    
}

- (void)handleCollectAlert{
    
}

- (void)handleDeleteAlert:(NSString *)str{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消收藏" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *delete = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *quxiao = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        [SQLHandler deleteData:str];
        [SQLHandler deleteDataForClassifyVC:str];
        
        [self handleData];
        [self.tableView reloadData];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"alreadyDeleteForClassifyVC" object:nil userInfo:nil];
    }];
    
    
    [alert addAction:quxiao];
    [alert addAction:delete];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        UIView *viewOfBackForSectionZero = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHT)];
        viewOfBackForSectionZero.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        return viewOfBackForSectionZero;
        
    }else{
        
        UIView *viewOfBackForSectionOne = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHT)];
        viewOfBackForSectionOne.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        return viewOfBackForSectionOne;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01 * HEIGHT;
}

- (void)handleBack{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.arrForMyExploreVC.count == 0 && self.arrForClassifyVC.count == 0) {
        return 1;
    }else{
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.arrForMyExploreVC.count == 0 && self.arrForClassifyVC.count == 0) {
        return 1;
    }else{
        
        if (section == 0) {
            return self.arrForMyExploreVC.count;
        }else{
            return self.arrForClassifyVC.count;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrForMyExploreVC.count == 0 && self.arrForClassifyVC.count == 0) {
        
        CellForCollectIsNull *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCollectIsNull"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else{
        if (indexPath.section == 0) {
            CellForCollect *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCollect"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.delegateForCollect = self;
            ModelOfCellForMyExploreTableViewSectionThreeAndFour *model = [self.arrForMyExploreVC objectAtIndex:indexPath.row];
            [cell passDataForCollect:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }else{
            CellForCollect *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCollect"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.delegateForCollect = self;
            ModelOfCellForMyExploreTableViewSectionThreeAndFour *model = [self.arrForClassifyVC objectAtIndex:indexPath.row];
            [cell passDataForCollect:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrForMyExploreVC.count == 0 && self.arrForClassifyVC.count == 0) {
        return 300;
    }else{
        return 82.8 * HEIGHT;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    [self handleData];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
